package com.jfinal.server.undertow.hotswap;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * ClassLoaderKit
 */
public class ClassLoaderKit {
    protected final HotSwapClassLoaderBuilder builder = new HotSwapClassLoaderBuilder();
    protected ClassLoader currentClassLoader;

    public ClassLoaderKit(Collection<URL> customClassPath, ClassLoader parentClassLoader, HotSwapResolver hotSwapResolver, boolean devMode) {
        builder.setParentClassLoader(parentClassLoader);
        builder.setHotSwapResolver(hotSwapResolver);
        builder.setDevMode(devMode);
        builder.addCustomClassPathUrls(customClassPath);

        replaceClassLoader();
    }

    public ClassLoader getClassLoader() {
        return currentClassLoader;
    }

    public void replaceClassLoader() {
        currentClassLoader = builder.build();
    }

    public static boolean isInFatJar() {
        return false;
    }

    public static List<URL> buildCustomClassPathForDeploy() {
        List<URL> customClassPath = new ArrayList<>();

        // todo
        final String rootPath = "";

        final Collection<File> paths = Collections.singletonList(
                new File(rootPath, "config")
        );

        paths.forEach(path -> {
            try {
                customClassPath.add(path.toURI().toURL());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        });

        return customClassPath;
    }

}





